home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / programming / c / supralib / developer / source.org / obtpens.c < prev    next >
C/C++ Source or Header  |  1999-06-14  |  4KB  |  110 lines

  1. /****** ObtPens ***************************************************
  2. *
  3. *   NAME
  4. *       ObtPens -- Obtain best pens from a list of colors (V10)
  5. *       (gfx V39)
  6. *
  7. *   SYNOPSIS
  8. *       number = ObtPens(cm, PalTable, PensTable, TagItem)
  9. *
  10. *       ULONG  = ObtPens(struct ColorMap *, ULONG *, ULONG *,
  11. *                 struct TagItem *);
  12. *
  13. *   FUNCTION
  14. *       This function calls ObtainBestPen() on a list of color
  15. *       entries, and puts results into a new pens list.
  16. *
  17. *       It will attempt to find colors in your viewport closest to
  18. *       the provided colors list (PalTable).
  19. *       This is usefull when you want to use an image with more
  20. *       specific colors on a public screen with a sharable palette.
  21. *
  22. *   INPUTS
  23. *       cm = colormap
  24. *       PalTable - list of RGB entries for each color you want to use.
  25. *                  The format of this table is the same as for LoadRGB32():
  26. *
  27. *             1 Word with the number of colors to obtain
  28. *             1 Word with the first color to be obtained
  29. *             3 longwords representing a left justified 32 bit RGB triplet
  30. *             The list is terminated by a count value of 0.
  31. *
  32. *             examples:
  33. *               ULONG PalTable[]={2l<<16+1,0,0,0, 0xffffffff,0,0, 0};
  34. *                   two entries (black, red); obtains only red one
  35. *
  36. *       PensTable - list of pen numbers on your viewport, obtained by
  37. *                   this function. First entry in PensTable will represent
  38. *                   the first color in PalTable, and so on.
  39. *                   NOTE that entries in PensTable with count number lower
  40. *                   than the first color to be obtained (provided in
  41. *                   PalTable) will be unaffected!
  42. *
  43. *       TagItem - this tagitem will be passed to ObtainBestPen() function,
  44. *                 that is called within ObtPens(). Please see ObtainBestPen()
  45. *                 in order to decide what kind of precision for obtaining
  46. *                 colors you will need. If this is NULL, PRECISION_IMAGE will
  47. *                 be used.
  48. *
  49. *   RESULT
  50. *       number = number of obtained colours (always the same as the first
  51. *       Word in PalTable), or 0 if failed. If it succeeds you must call
  52. *       RelPens() to free obtained colors.
  53. *
  54. *
  55. *   EXAMPLES
  56. *
  57. *       The following example will obtain red, green and blue colours in a
  58. *       viewport, and will put obtained pens into pens[] table. pens[0] will
  59. *       be untouched (will remain the same colour as viewport's background).
  60. *
  61. *       ULONG pal[((4<<16)+1, \* 4 entries, starting with the second one *\
  62. *                   0, 0, 0,          \* black - will ignore this one *\
  63. *                   0xffffffff, 0, 0, \* red *\
  64. *                   0, 0xffffffff, 0, \* green *\
  65. *                   0, 0, 0xffffffff, \* blue *\
  66. *                   0};
  67. *
  68. *       ULONG pens[4];
  69. *
  70. *       ObtPens(cm, pal, pens, NULL);
  71. *
  72. *       SetAPen(rp, pens[1]);   \* Set the primary pen to red *\
  73. *       Text(rp, "I'm red!", 8);
  74. *
  75. *
  76. *   NOTES
  77. *       You MUST call RelPens() to free all obtained colors if ObtPens()
  78. *       have succeeded, but you must not call it if ObtPens() returns 0.
  79. *       You MUST open graphics library (V39 or higher) before calling this
  80. *       function.
  81. *
  82. *   SEE ALSO
  83. *       RelPens(), ObtainBestPen(), LoadRGB32()
  84. *
  85. *************************************************************************/
  86.  
  87. #include <proto/graphics.h>
  88. #include <libraries/supra.h>
  89.  
  90. ULONG ObtPens(struct ColorMap *cm, ULONG *paltab, ULONG *pens, struct TagItem *tags)
  91. {
  92. int i,j;
  93. int num = paltab[0]>>16;
  94.  
  95.     for (i = (WORD)*((WORD *)paltab+1); i<num; i++) {
  96.         if ((pens[i] = ObtainBestPenA(cm, paltab[3*i+1],
  97.                                          paltab[3*i+2],
  98.                                          paltab[3*i+3],
  99.                                          tags)) == -1) {
  100.  
  101.                                             for (j=(WORD)*((WORD *)paltab+1); j<i; j++) ReleasePen(cm, pens[j]);
  102.                                             return(0);
  103.                                          }
  104.  
  105.     }
  106.  
  107.     return((ULONG)num);
  108. }
  109.  
  110.